package cn.zxyh.controller;

import cn.zxyh.pojo.User;
import cn.zxyh.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author:man
 * @date:2020/5/25
 */
@RestController
public class UserController {
    @Autowired
    Userservice userservice;


    @Autowired
    RedisTemplate redisTemplate;


    /*
     *
     */
    @GetMapping(path = "/user")
    public Map<String, Object> userInfo(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        Map<String, Object> data = new HashMap<>();
//        data.put("password", user.getPassword());
        data.put("card_number", user.getCardNumber());
        data.put("username", user.getUsername());
        result.put("data", data);
        return result;
    }

    /*
     * 更改密码
     */
    @PostMapping(path = "/user/modifypassword",consumes="application/json")
    public Map<String, Object> modifyUserPassword(@RequestBody Map<String,String> map,HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        String password = map.get("password");
        String token = request.getParameter("access_token");
        // TODO 判空？
        boolean flag = false;
        if (password != null && !"".equals(password)) {
            user.setPassword(password);
            int res = userservice.updateUser(user);
            redisTemplate.opsForValue().set(token, user, 30, TimeUnit.MINUTES);
            flag = res > 0;
        }

        Map<String, Object> result = new HashMap<>();
        if (flag) {
            result.put("code", 0);
            result.put("msg", "modify password success!");
            return result;
        }

        if (password == null || "".equals(password)) {
            result.put("code", 1);
            result.put("msg", "password is empty");
            return result;
        }

        result.put("code", 1);
        result.put("msg", "modify password failed!");
        return result;
    }

//银行卡更新

    @PostMapping(path = "/user/modifybankcard")
    public  Map<String,Object> bankcard(@RequestBody Map<String,String> paraMap,HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        String bankcard = paraMap.get("bankcard");
        String token = request.getParameter("access_token");
        user.setCardNumber(bankcard);
        int status = userservice.modifyBankCard(user);
        if (status != 0)
            redisTemplate.opsForValue().set(token,user,30,TimeUnit.MINUTES);
        Map<String,Object> map = new HashMap<>();
        if (status != 0){
            map.put("code",0);
            map.put("msg","修改成功");
        }
        else {
             map.put("code",1);
             map.put("msg","修改失败");
            }
        return map;

    }


}